<?php
/**
 * Created by PhpStorm.
 * User: wpf
 * Date: 2018/4/21
 * Time: 15:20
 */

namespace User\Controller;

use Common\Controller\MemberbaseController;
class StoreController extends MemberbaseController
{

    public function get_bunk() {
        $pid    = I('pid', 0, 'int');
        if(!$pid){
            return;
        }
        $list   = M('Bunk')->where(array('pid'=>$pid))->select();
        $result = array();
        foreach($list as $k => $v) {
            array_push($result, $v);
        }
        echo json_encode($result);
    }

    public function addbunk()
    {
        $cardnum = I('cardnum');
        if ($cardnum) {
            $userinfo = D('userinfo')->relation(true)->where(array('cardnum' => $cardnum))->find();
            if ($userinfo) {
                $area = M('Bunk')->where(array('type'=>1))->select();//房间号还是需要查出所有内容

                //如果用户金额添加时 存在有已选择的床位信息，则应该为继续操作该床位信息！
                //6.22沟通，   每次金额变动的时候，都需要重新进行选择，无需记录上次选择内容
                //没有选过 房间号
                $house = M('Bunk')->where(array('pid'=>2))->select();

                $this->assign('house',$house);
                $this->assign('area',$area);
                $this->assign('userinfo', $userinfo);
                $this->assign('cardnum', $cardnum);
                $this->display();
            } else {
                $this->error('请输入正确卡号！');
            }
        } else {
            $this->assign('cardnum', '');
            $this->display();
        }
    }

    public function doaddbunk()
    {
        if ($_POST) {
            $cardnum = I('cardnum');
            $name = I('name','','trim');
            //6.21增加床位选择
            $area = I('area');
            $areas = explode("~",$area);
            $house = I('house');
            $houses = explode("~",$house);
            $floor = I('floor');
            $floors = explode("~",$floor);
            $room = I('room');
            $rooms = explode("~",$room);
            $bed = I('bed');
            $beds = explode("~",$bed);
            $use = I('use');//本次使用时间
            //6.22增加 床位再次选择原床位内容的处理
            $usetime = I('usetime');//上次使用时间， 需要在对应的床位进行扣除
            $oldbunkid = I('oldbunkid');

            if($use){
                if(!$beds[0]){
                    $this->error('未选择房间不可添加月份');
                }
            }

            $data = array(
                'cardnum'   => $cardnum,
                'name'      => $name,
                'paytype'   => '床位添加',
                'storetype' => 1,
                'createby'  => $this->user['id'],
                'createbyuser'=> $this->user['user_nicename'],
                'createtime'=>date('Y-m-d H:i:s'),
                'bunkid' => $beds[0],
                'usetime' => $use
            );
            //需要对床位的变动信息进行增加
            M('store')->add($data);

            M('userinfo')->where(array('cardnum'=>$cardnum))
                ->save(
                    array(
                        'areaname' =>$areas['1'],
                        'areaid' =>$areas['0'],
                        'housename' =>$houses['1'],
                        'houseid' =>$houses['0'],
                        'floorname' =>$floors['1'],
                        'floorid' =>$floors['0'],
                        'roomname' =>$rooms['1'],
                        'roomid' =>$rooms['0'],
                        'bedname' =>$beds['1'],
                        'bedid' =>$beds['0'],
                        'usetime' => $use
                    )
                );
                //bunk表应当需要更新两条语句，第一条是之前选择的床位 时间空余去除
                if($oldbunkid) {
                    M('bunk')->execute("update cmf_bunk set used=used-$usetime where bunk_id=$oldbunkid");
                }
                //第二个为当前床位信息进行叠加，真正的时间为叠加后的时间！
                if($beds[0]) {
                    M('bunk')->execute("update cmf_bunk set used=used+$use where bunk_id=$beds[0]");
                }
                //M('bunk')->where(array('bunk_id'=>$beds['0']))->save(array('used'=>$usetime));
                $this->success('添加成功！',UU('User/Info/index',array('cardnum'=>$cardnum)));

        }else{
            $this->error('请提交正确信息！！');
        }
    }

    public function add()
    {
        $cardnum = I('cardnum');
        if ($cardnum) {
            $userinfo = D('userinfo')->relation(true)->where(array('cardnum' => $cardnum))->find();
            if ($userinfo) {
                $area = M('Bunk')->where(array('type'=>1))->select();//房间号还是需要查出所有内容

                //如果用户金额添加时 存在有已选择的床位信息，则应该为继续操作该床位信息！
                //6.22沟通，   每次金额变动的时候，都需要重新进行选择，无需记录上次选择内容
                    //没有选过 房间号
                $house = M('Bunk')->where(array('pid'=>2))->select();

                $this->assign('house',$house);
                $this->assign('area',$area);
                $this->assign('userinfo', $userinfo);
                $this->assign('cardnum', $cardnum);
                $this->display();
            } else {
                $this->error('请输入正确卡号！');
            }
        } else {
            $this->assign('cardnum', '');
            $this->display();
        }
    }

    public function doadd()
    {
        if ($_POST) {
            $cardnum = I('cardnum');
            $name = I('name','','trim');
            $totalstore = I('usedstore',0,'float');
            $remark = I('remark');
            $costcount = I('costcount',0,'int');

            $storedmoney = I('storedmoney', 0, 'float');
            $storedsendmoney = I('storedsendmoney', 0, 'float');

            //6.21增加床位选择
            $area = I('area');
            $areas = explode("~",$area);
            $house = I('house');
            $houses = explode("~",$house);
            $floor = I('floor');
            $floors = explode("~",$floor);
            $room = I('room');
            $rooms = explode("~",$room);
            $bed = I('bed');
            $beds = explode("~",$bed);
            $use = I('use');//本次使用时间

            if($use){
                if(!$beds[0]){
                    $this->error('未选择房间不可添加月份');
                }
            }

            //6.22增加 床位再次选择原床位内容的处理
            $usetime = I('usetime');//上次使用时间， 需要在对应的床位进行扣除
            $oldbunkid = I('oldbunkid');

            //12.21 增加 积分内容 此积分内容为 充值后附送的内容,可以在购买东西时进行扣除
            //$integral = I('integral');
            //积分内容需要进行累加,获取上次积分的值,进行叠加
            //$oldintegral = I('oldintegral');
            //$integral += $oldintegral;

            if($storedmoney<=0){
                $this->error('请输入正确的充值金额！！');
            }

            if($storedsendmoney<0){
                $this->error('请输入正确的赠送金额！！');
            }

            $storedmoney = $storedmoney * 100;
            $storedsendmoney = $storedsendmoney * 100;
            $laststore = $totalstore * 100;
            $totalstore = $laststore + $storedmoney + $storedsendmoney;

            $data = array(
                'cardnum'   => $cardnum,
                'name'      => $name,
                'laststore' => $laststore,
                'remark'    => $remark,
                'paytype'   => '会员充值',
                'storetype' => 1,
                'createby'  => $this->user['id'],
                'createbyuser'=> $this->user['user_nicename'],
                'nowstore'  => $totalstore,
                'storemoney'=> $storedmoney,
                'storesendmoney'=> $storedsendmoney,
                'createtime'=>date('Y-m-d H:i:s')
            );
            if($beds[0]){
                $data['bunkid'] = $beds[0];
                $data['usetime'] = $use;
            }

            //由于历史原因， 房间选择暂时不能添加到 记录历史中，房间添加功能需要  在  用户列表中添加未进行关联的用户，
            //在用户列表中添加 房间信息，那么不会和历史充值记录参数关联，只在userinfo表中 作为显示的信息
            //添加到用户表中的时候，添加一个对应的id及名称信息， 共6个字段！ 外加使用期权  7个字段

            //考虑一个问题！！！ 如果id换了，那么原id的   月数是否相对应的增加上来， 或者去除
//            A 使用   1号床位  30月
//            B 使用  1号     60月
//            总共 90个月，  后1号续费了，怎么办？？？？-->   需要重新选择内容吗？  如果改变了 内容是否需要重新
//            现思路，如果要是更改了床位id，那么我们就重新记录这些信息（其中 当前使用的月份重新进行更新减去或者？）， 如果没有更改id那么只用 更新 月份
            //6.22沟通结果，  只要金额变动，那么我们就需要重新进行选择与计算，
            if(M('store')->add($data)){
                M('userinfo')->where(array('cardnum'=>$cardnum))
                    ->save(
                        array(
                            'usedstore'=>$totalstore,
                            'totalstore'=>$totalstore,
                            'lastcosttime'=>date('Y-m-d H:i:s'),
                            'costcount'=>$costcount+1,
                            'areaname' =>$areas['1'],
                            'areaid' =>$areas['0'],
                            'housename' =>$houses['1'],
                            'houseid' =>$houses['0'],
                            'floorname' =>$floors['1'],
                            'floorid' =>$floors['0'],
                            'roomname' =>$rooms['1'],
                            'roomid' =>$rooms['0'],
                            'bedname' =>$beds['1'],
                            'bedid' =>$beds['0'],
                            'usetime' => $use
                            //'integral'=>$integral
                        )
                    );
                /*
                if($integral){
                    //添加初始化充值内容 只有充值的时候,进行积分的叠加,  积分的消费,在子账号中进行
                    //每次都是新的积分增加
                    M('integral')->add(
                            array(
                                'cardnum'=>$cardnum,
                                'type'=> 1,
                                'name'=>$name,
                                'beforeintegral'=>$oldintegral,
                                'integralchange'=>$integral-$oldintegral,
                                'nowintegral'=>$integral,
                                'createtime'=>date('Y-m-d H:i:s'),
                                'createby'  => $this->user['id'],
                                'createbyuser'=> $this->user['user_nicename'],
                                'remark' => '会员充值赠送的积分'
                            )
                        );
                }
                */
                //bunk表应当需要更新两条语句，第一条是之前选择的床位 时间空余去除
                if($oldbunkid) {
                    M('bunk')->execute("update cmf_bunk set used=used-$usetime where bunk_id=$oldbunkid");
                }
                //第二个为当前床位信息进行叠加，真正的时间为叠加后的时间！
                if($beds[0]) {
                    M('bunk')->execute("update cmf_bunk set used=used+$use where bunk_id=$beds[0]");
                }
                //M('bunk')->where(array('bunk_id'=>$beds['0']))->save(array('used'=>$usetime));

               if($this->user['url_type'] == 2){
                   $this->success('会员充值成功！',UU('User/store/reduce'));
               }else{
                   $this->success('会员充值成功！',UU('User/Info/index',array('cardnum'=>$cardnum)));
               }

            }
        }else{
            $this->error('请提交正确信息！！');
        }
    }

    public function reduce()
    {
        $cardnum = I('cardnum');
        if ($cardnum) {
            $userinfo = D('userinfo')->relation(true)->where(array('cardnum' => $cardnum))->find();
            if ($userinfo) {
                $area = M('Bunk')->where(array('type'=>1))->select();//房间号还是需要查出所有内容
                $house = M('Bunk')->where(array('pid'=>2))->select();

                $this->assign('house',$house);
                $this->assign('area',$area);
                $this->assign('userinfo', $userinfo);
                $this->assign('cardnum', $cardnum);
                $this->display();
            } else {
                $this->error('请输入正确卡号！');
            }
        } else {
            $this->assign('cardnum', '');
            $this->display();
        }
    }

    public function doreduce(){
        if ($_POST) {
            $cardnum = I('cardnum');
            $name = I('name');
            $totalstore = I('usedstore',0,'float');
            $remark = I('remark');
            $storedmoney = I('storedmoney', 0, 'float');
            $costcount = I('costcount',0,'int');

            //6.21增加床位选择
            $area = I('area');
            $areas = explode("~",$area);
            $house = I('house');
            $houses = explode("~",$house);
            $floor = I('floor');
            $floors = explode("~",$floor);
            $room = I('room');
            $rooms = explode("~",$room);
            $bed = I('bed');
            $beds = explode("~",$bed);
            $use = I('use');//本次使用时间

            if($use){
                if(!$beds[0]){
                    $this->error('未选择房间不可添加月份');
                }
            }

            //6.22增加 床位再次选择原床位内容的处理
            $usetime = I('usetime');//上次使用时间， 需要在对应的床位进行扣除
            $oldbunkid = I('oldbunkid');

            if($storedmoney<=0){
                $this->error('请输入正确的充值金额！！');
            }

            $storedmoney = $storedmoney * 100;

            $laststore = $totalstore * 100;

            $nowstore = $laststore - $storedmoney ;

            if($nowstore<0){
                $this->error('卡上余额不足，操作失败！！');
            }

            $data = array(
                'cardnum'   => $cardnum,
                'name'      => $name,
                'laststore' => $laststore,
                'remark'    => $remark,
                'paytype'   => '储值支付',
                'storetype' => 2,
                'createby'  => $this->user['id'],
                'createbyuser'=> $this->user['user_nicename'],
                'nowstore'  => $nowstore,
                'storemoney'=> $storedmoney,
                'createtime'=>date('Y-m-d H:i:s'),
            );

            if($beds[0]){
                $data['bunkid'] = $beds[0];
                $data['usetime'] = $use;
            }

            if(M('store')->add($data)){

                $saveData = array(
                    'usedstore'=>$nowstore,
                    'totalstore'=>$nowstore,
                    'lastcosttime'=>date('Y-m-d H:i:s'),
                    'costcount'=>$costcount+1
                );
                if($nowstore!=0){
                    $saveData =$saveData+array(
                        'areaname' =>$areas['1'],
                        'areaid' =>$areas['0'],
                        'housename' =>$houses['1'],
                        'houseid' =>$houses['0'],
                        'floorname' =>$floors['1'],
                        'floorid' =>$floors['0'],
                        'roomname' =>$rooms['1'],
                        'roomid' =>$rooms['0'],
                        'bedname' =>$beds['1'],
                        'bedid' =>$beds['0'],
                        'usetime' => $use
                     );

                    //bunk表应当需要更新两条语句，第一条是之前选择的床位 时间空余去除
                    if($oldbunkid) {
                        M('bunk')->execute("update cmf_bunk set used=used-$usetime where bunk_id=$oldbunkid");
                    }
                    //第二个为当前床位信息进行叠加，真正的时间为叠加后的时间！
                    if($beds[0]) {
                        M('bunk')->execute("update cmf_bunk set used=used+$use where bunk_id=$beds[0]");
                    }
                }else{
                    //如果金额扣到0 ，则需要清除数据
                    $saveData =$saveData+array(
                            'areaname' =>null,
                            'areaid' =>null,
                            'housename' =>null,
                            'houseid' =>null,
                            'floorname' =>null,
                            'floorid' =>null,
                            'roomname' =>null,
                            'roomid' =>null,
                            'bedname' =>null,
                            'bedid' =>null,
                            'usetime' => null
                        );
                    if($oldbunkid) {
                        M('bunk')->execute("update cmf_bunk set used=used-$usetime where bunk_id=$oldbunkid");
                    }
                }

                M('userinfo')->where(array('cardnum'=>$cardnum))
                    ->save($saveData);
               // $this->success('会员扣费成功！',UU('User/Info/index',array('cardnum'=>$cardnum)));
                if($this->user['url_type'] == 2){
                    $this->success('会员扣费成功！',UU('User/store/reduce'),2);
                }else{
                    $this->success('会员扣费成功！',UU('User/Info/index',array('cardnum'=>$cardnum)));
                }

            }

        }else{
            $this->error('请提交正确信息！！');
        }
    }



}